﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Web;
using System.Web.Mvc;
using OPM.Class;
using OPM.Models;
using OPM.Models.BaseModel;

namespace OPM.Controllers
{
    public class OfficerProfileLogsController : Controller
    {
        //
        // GET: /OfficerProfileLogs/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Profile()
        {
            try
            {
                GetGender();
                Organize();
                var profile = WSO.GetAllProfile();  
                profile.picture_profile = WSO.GetPictureProfile(U.Token);
                return View(profile);
            }
            catch (Exception ex)
            {
                return View();
            }

        }

        public void Organize()
        {
            var organize = WSL.GetOrganization("");
            ViewBag.organize = new SelectList(organize, "VAL", "TEXT");
        }

        [HttpPost]
        public ActionResult Profile(OfficerProfile model, HttpPostedFileBase file)
        {
            OfficerProfile officerProfile = null;
            string pictureProfileResult = "";
            string pictureName = "";
            string pictureContent = ""; 
            GetGender();
            Organize();


            try
            {
                var token = Wb.GetToken(U.CurrentUser.user_name, model.password);

                if (token != null)
                {
                    var isPictured = false;
                    pictureName = "";
                    pictureContent = "";
                    if (file != null && file.InputStream.Length > 0)
                    {
                        isPictured = true;
                        byte[] buffer = new byte[file.InputStream.Length];
                        file.InputStream.Read(buffer, 0, (int)file.InputStream.Length);
                        pictureContent = Convert.ToBase64String(buffer);
                        pictureName = file.FileName;
                    }

                    officerProfile = WSO.UpdateProfile(U.Token, model.gender_id, model.salutation_th, model.firstname_th, model.lastname_th,
                          model.salutation_en, model.firstname_en, model.lastname_en, model.other_name, Util.ConvertDateTimeToWebService(DateTime.Now), model.citizen_id,
                          model.mobile, model.phone, model.email, model.position, U.role_id, model.org_id, model.address,
                          model.subdistrict_id, model.district_id, model.province_id, "12111", model.default_language, model.row_per_page,
                          pictureContent, pictureName, "", "");

                    pictureProfileResult = WSO.SetPictureProfile(U.Token, pictureName, pictureContent);

                    

                    User user = null;
                    if (!string.IsNullOrWhiteSpace(U.CurrentUser.user_name) &&
                   (user = Wb.GetUser(U.CurrentUser.user_name, model.password)) != null)
                    {
                        U.LoginKeepSession(user);
                        string cultureChanged = (model.default_language ?? "TH").ToUpper().StartsWith("EN") ? "EN" : "TH";
                        Session["culture"] = cultureChanged;
                        System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureChanged);
                    }
                    
                }
                else
                {
                    TempData["NonSuccess"] = "คุณกรอกรหัสผ่านไม่ถูกต้อง";
                    return View(model);
                }
            }
            catch (Exception ex)
            {
                TempData["NonSuccess"] = "ตรวจสอบข้อมูลอีกครั้ง" + ex.Message;
                return View(model);
            }

            return RedirectToAction("Index", "PopupResult");
        }

        public void GetGender()
        {
            var listOfGender = WSL.GetGender();
            ViewBag.listOfGender = new SelectList(listOfGender, "VAL", "TEXT"); 
        }

        public ActionResult EditProfile()
        {
            return View();
        }

        public ActionResult ProfileLogs()
        {
           var profilelog = WSO.GetOfficerProfileLogs(U.Token, U.officer_id, 0, 10);
           ViewBag.profilelog = profilelog;
           return View(profilelog);
        }


    }
}
